package com.situ.crm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Product;
import com.situ.crm.entity.SaleChance;
import com.situ.crm.entity.User;
import com.situ.crm.entity.UserRole;
import com.situ.crm.mapper.SaleChanceMapper;
import com.situ.crm.mapper.UserRoleMapper;
import com.situ.crm.service.ISaleChanceService;
import com.situ.crm.util.UserContext;

@Service
public class SaleChanceSserviceImpl implements ISaleChanceService {
	@Autowired
	private SaleChanceMapper saleChanceMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;

	@Override
	public ServerResponse pageList(Integer page, Integer limit, SaleChance saleChance) {
		PageHelper.startPage(page,limit);
		//查询如果是销售人员，只能查看自己的营销记录
		List<SaleChance> list = new ArrayList<>();
		User user = (User) UserContext.session.getAttribute(UserContext.USER_IN_SESSION);
		Integer xiaoShouRoleCount  = userRoleMapper.selectXiaoShouRoleCountByUserId(user.getId());
		// 如果用户是销售角色，就只能查看自己下面的营销机会
		if (xiaoShouRoleCount > 0) {
			list = saleChanceMapper.selectByUserId(user.getId());
		} else {
			list = saleChanceMapper.pageList(saleChance);
		}
		
		PageInfo pageInfo = new PageInfo<>(list);
		Integer count = (int)pageInfo.getTotal();
		return ServerResponse.createSuccess("查找成功", count, list);
	}

	//添加
	@Override
	public ServerResponse insert(SaleChance saleChance) {
		try {
			Integer count = saleChanceMapper.insert(saleChance);
			if (count == 1) {
				return ServerResponse.createSuccess("添加成功");
			} else {
				return ServerResponse.createError("添加失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("添加失败");
		}
	}

	//根据Id删除
	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			Integer count = saleChanceMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("添加成功");
			} else {
				return ServerResponse.createError("添加失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("添加失败");
		}
	}

	//批量删除
	@Override
	public ServerResponse deleteAll(String ids) {
		String[] arrays = ids.split(",");
		try {
			Integer count = saleChanceMapper.deleteAll(arrays);
			if (count >= 1) {
				return ServerResponse.createSuccess("添加成功");
			} else {
				return ServerResponse.createError("添加失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("添加失败");
		}
	}

	//修改查询
	@Override
	public SaleChance select(Integer id) {
		return saleChanceMapper.selectByPrimaryKey(id);
	}

	//修改
	@Override
	public ServerResponse update(SaleChance saleChance) {
		try {
			Integer count = saleChanceMapper.updateByPrimaryKeySelective(saleChance);
			if (count == 1) {
				return ServerResponse.createSuccess("修改成功");
			} else {
				return ServerResponse.createError("修改失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("修改失败");
		}
	}

	//根据sale_chance_id查出sale_chance表的数据
	@Override
	public SaleChance selectById(Integer id) {
		return saleChanceMapper.selectByPrimaryKey(id);
	}

	//添加功能,同时把未开发设置为开发中
	@Override
	public void updateDevResult(Integer id) {
		saleChanceMapper.updateDevResult(id);
	}
}
